package com.sword.service.impl.system;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.sword.constant.MessageConstant;
import com.sword.mapper.system.EmployeeMapper;
import com.sword.page.PageResult;
import com.sword.page.QueryPageBean;
import com.sword.pojo.system.Employee;
import com.sword.service.impl.BasicBaseServiceImpl;
import com.sword.service.system.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Field;
import java.util.Date;
import java.util.List;

@Service
public class EmployeeServiceImpl extends BasicBaseServiceImpl<Employee> implements EmployeeService {
//    @Override
//    public PageResult queryEmployee(QueryPageBean queryPageBean) {
//        QueryWrapper<Employee> wrapper = new QueryWrapper<>();
//
//        IPage<Employee> iPage = super.queryPageList(wrapper, queryPageBean.getPage(), queryPageBean.getLimit());
//
//        return new PageResult(0, MessageConstant.QUERY_EMPLOYEE_SUCCESS, iPage.getTotal(), iPage.getRecords());
//    }
//
//    @Override
//    public PageResult queryEmployeeLike(QueryPageBean queryPageBean) {
//        QueryWrapper<Employee> wrapper = new QueryWrapper<>();
//        Employee employee = new Employee();
//        Field[] fields = employee.getClass().getDeclaredFields();
//        for (int i = 0; i < fields.length; i++) {
//            //有的字段是用private修饰的 将他设置为可读
//            fields[i].setAccessible(true);
//            wrapper.like(fields[i].getName(),queryPageBean.getQueryString()).or();
//        }
//        IPage<Employee> iPage = super.queryPageList(wrapper, queryPageBean.getPage(), queryPageBean.getLimit());
//
//        return new PageResult(0, MessageConstant.QUERY_EMPLOYEE_SUCCESS, iPage.getTotal(), iPage.getRecords());
//    }
//
//    @Override
//    public Integer addEmployee(Employee employee) {
//        employee.setBirthDate(new Date());
//        employee.setHireDate(new Date());
//        employee.setMatureDate(new Date());
//        return super.save(employee);
//    }
//
//    @Override
//    public Integer editEmployee(Employee employee) {
//        return super.update(employee);
//    }
//
//    @Override
//    public Integer deleteEmployee(Integer id) {
//        return super.deleteById(Long.valueOf(id));
//    }
    @Autowired
    EmployeeMapper employeeMapper;

    @Override
    public Integer addEmployee(String deptid, String empolyeeid, String password,
                           String employeename, String duty, String Gender,
                           Date BirthDate, Date HireDate, Date MatureDate, String Phone,
                           String Address, String Email) {
        Employee employee = new Employee(deptid, empolyeeid, password, employeename,
                                        duty, Gender, BirthDate,HireDate, MatureDate, Phone,
                                        Address, Email);
        Integer result = employeeMapper.insert(employee);
        return result;
    }

    @Override
    public Integer deleteEmployee(Integer id) {
//        QueryWrapper<Employee> wrapper = new QueryWrapper<>();
//        wrapper.eq("employeeid",employeeid);
//        int result =  employeeMapper.delete(wrapper);
        Integer result = employeeMapper.deleteById(id);
        return result;
    }

    @Override
    public Integer deleteEmployees(List<Integer> id) {
        Integer result = employeeMapper.deleteBatchIds(id);
        return result;
    }

    @Override
    public Integer modifyEmployee(Integer id,String deptid, String empolyeeid, String password,
                                  String employeename, String duty, String Gender,
                                  Date BirthDate, Date HireDate, Date MatureDate, String Phone,
                                  String Address, String Email) {
                Employee employee = new Employee(id,deptid, empolyeeid, password, employeename,
                                        duty, Gender, BirthDate,HireDate, MatureDate, Phone,
                                        Address, Email);
        Integer result = employeeMapper.updateById(employee);
        return result;
    }


    @Override
    public List<Employee> queryAll() {
        QueryWrapper<Employee> wrapper = new QueryWrapper<>();
        wrapper.isNotNull("id");
        List<Employee> employees = employeeMapper.selectList(wrapper);
        return employees;
    }

    @Override
    public Employee queryEmployeeById(String id) {
        QueryWrapper<Employee> wrapper = new QueryWrapper<>();
        wrapper.eq("employeeid",id);
        Employee employee = employeeMapper.selectOne(wrapper);
        return employee;
    }

    @Override
    public List<Employee> queryEmployeeLike(String word) {
        QueryWrapper<Employee> wrapper = new QueryWrapper<>();
        wrapper
                .like("deptid",word)
                .like("employeeid",word)
                .like("Employee_Name",word)
                .like("Duty",word)
                .like("Gender",word)
                .like("BirthDate",word)
                .like("HireDate",word)
                .like("MatureDate",word)
                .like("Phone",word)
                .like("Address",word)
                .like("Email",word);
        List<Employee> employees = employeeMapper.selectList(wrapper);
        return employees;
    }
}
